/* File Created: 三月 19, 2014 */
(function ($) {
    $.login = {};
    $.extend($.login,
    {
        div_mask: "#J_mask",
        oPhone: "#J_phone", //手机号
        oSMSCode: "#J_yzm_txt",
        oSendSMSBtn: "#btn_getcode",
        btnSubmit: "#btn-login", //提交按钮
        oContract: "#i_contract",
        callback: null,
        init: function (options) {
            $.extend($.login, options);
            //从cookie中读取用户记录
            $.login.utils.recoverInput();

            $("#J_loginfail_pop").find("a").click(function () {
                $("#J_loginfail_pop .pop-close").click();
            });
            //绑定登录按钮
            $($.login.btnSubmit).click($.login.submitOrderFlow.submitOrder);
            $($.login.oSendSMSBtn).click($.login.sms.sendsms);
        },
        utils: {
            saveInput: function () {
                //保存订单中提交的数据
                var saveValue = {
                    "phone": encodeURI($($.login.oPhone).val())
                };
                llb_global.utils.setCookie("phone", JSON.stringify(saveValue));
            },
            recoverInput: function () {
                if (llb_global.utils.readcookie("phone") !== "") {
                    try {
                        var cb = JSON.parse(llb_global.utils.readcookie("phone"));
                        if (typeof (cb) !== "undefined") {
                            if (typeof (cb.phone) !== "undefined") {
                                $($.login.oPhone).val(decodeURI(cb.phone));
                            }
                        }
                    } catch (e) { }
                }
            }
        },
        //验证方法集合
        validateFn: {
            checkPhone: function () {
                return $.validator.validate("phone", {
                    el: $($.login.oPhone),
                    allowEmpty: false,
                    failCallback: function (err) {
                        if (err) {
                            tips.setError(err);
                        }
                    }
                }).IsPass();
            },
            checkCode: function () {
                return $.validator.validate({
                    el: $($.login.oSMSCode),
                    failCallback: function (err) {
                        if (err) {
                            tips.setError(err);
                        }
                    },
                    customCheck: function () {
                        var vr = $.validator.createResult();
                        var code = $($.login.oSMSCode).val();
                        if (!code.trim()) {
                            vr.errMsg = "请输入验证码";
                            vr.success = false;
                        } else {
                            vr.success = true;
                        }
                        return vr;
                    }
                }).IsPass();
            }
        },
        validate: function () {
            var success = true;
            for (var f in this.validateFn) {
                if (this.validateFn.hasOwnProperty(f)) {
                    success = this.validateFn[f]() && success;
                }
            }
            return success;
        },
        sms: {
            sendsms: function () {
                tips.clearError();
                if (!$.login.validateFn.checkPhone()) {
                    tips.show(tips.getError());
                    return false;
                }
                $.login.utils.saveInput();
                wxApiAuth(function (openid) {
                    if (openid) {
                        llb_global.utils.setCookie("openid", openid);
                    }
                    $.ajax({
                        type: "GET",
                        url: llb_global.config.wxapi + "ajax_h5_get_sms_code&mobile=" + $($.login.oPhone).val().trim(),
                        xhrFields: {
                            withCredentials: true
                        },
                        success: function (data) {
                            var sc = 30;
                            $($.login.oSendSMSBtn).unbind("click").text(sc + "秒后重发");
                            var wt = setInterval(function () {
                                if (parseInt(sc, 10) > 1) {
                                    sc -= 1;
                                    $($.login.oSendSMSBtn).unbind("click").text(sc + "秒后重发");
                                } else {
                                    clearInterval(wt);
                                    $($.login.oSendSMSBtn).text("发送验证码").click(function () { $.login.sms.sendsms(); });
                                }
                            }, 1000);
                        },
                        error: function (e) {
                            //alert("短信验证码发送失败");
                        }
                    });
                });
            }
        },

        __formData: {},
        submitOrderFlow: {
            completed: function () {
                $($.login.btnSubmit).off("click").click($.login.submitOrderFlow.submitOrder);
            },
            submitOrder: function () {
                var errMasker = tips;
                errMasker.clearError();
                if (!$.login.validate()) {
                    errMasker.show();
                    return;
                }
                $($.login.btnSubmit).off("click");
                $.login.__formData = {
                    phone: $($.login.oPhone).val(),
                    smscode: $($.login.oSMSCode).val(),
                    openid: llb_global.utils.readcookie("openid")
                };
                var inviteInfo_json = llb_global.utils.readcookie("inviteInfo");

                if (inviteInfo_json !== "" && typeof (inviteInfo_json) !== "undefined") {
                    try {
                        var inviteInfo = JSON.parse(inviteInfo_json);
                        $.login.__formData.invitePhone = inviteInfo.mobile;
                        $.login.__formData.inviteOpenID = inviteInfo.openid;
                    } catch (e) { }
                }

                $.ajax({
                    type: "GET",
                    url: llb_global.config.api + "api/Coupon/Login",
                    timeout: 2000,
                    data: $.login.__formData,
                    dataType: 'jsonp',
                    jsonp: "callback",
                    jsonpCallback: "jsonpcallback.LoginCallback"
                });
            }

        }
    });
})(jQuery);
